/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminUpdateRestaurantGui.java
 *
 * Created on 4 Φεβ 2012, 8:29:39 μμ
 */

package gui;

import HelperClasses.Database.AllRestaurants;
import HelperClasses.Messages.InformationMessage;
import HelperClasses.Messages.TerminateQuestion;
import entertainmentguide.entity.User;
import javax.swing.JFrame;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

/**
 *
 * 
 */
public class AdminUpdateRestaurantGui extends javax.swing.JFrame {
    
    // ο τρέχων χρήστης
    static public  User loggedUser = new User(); 
    
    /** Creates new form AdminUpdateRestaurantGui */
    public AdminUpdateRestaurantGui() {
        initComponents();
        hideIdFromTable();
        
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LogoLabel = new javax.swing.JLabel();
        BackgroundColorPanel = new javax.swing.JPanel();
        AddRestaurantButton = new javax.swing.JButton();
        UpdateRestaurantButton = new javax.swing.JButton();
        DeleteRestaurantButton = new javax.swing.JButton();
        ExitButton = new javax.swing.JButton();
        TitleLabel = new javax.swing.JLabel();
        UpdateScrollPane = new javax.swing.JScrollPane();
        UpdateTable = new javax.swing.JTable();
        UpdateButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Restaurant Guide");
        setMinimumSize(new java.awt.Dimension(800, 800));
        setPreferredSize(new java.awt.Dimension(800, 800));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);

        LogoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/thessaloniki-restaurants.jpg"))); // NOI18N
        LogoLabel.setVerifyInputWhenFocusTarget(false);
        getContentPane().add(LogoLabel);
        LogoLabel.setBounds(0, 0, 400, 120);

        BackgroundColorPanel.setBackground(new java.awt.Color(255, 153, 102));
        BackgroundColorPanel.setMinimumSize(new java.awt.Dimension(180, 500));
        BackgroundColorPanel.setPreferredSize(new java.awt.Dimension(180, 500));
        BackgroundColorPanel.setLayout(null);

        AddRestaurantButton.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        AddRestaurantButton.setText("Εισαγωγή Νέου");
        AddRestaurantButton.setEnabled(false);
        AddRestaurantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddRestaurantButtonActionPerformed(evt);
            }
        });
        BackgroundColorPanel.add(AddRestaurantButton);
        AddRestaurantButton.setBounds(10, 38, 153, 21);

        UpdateRestaurantButton.setBackground(new java.awt.Color(255, 255, 0));
        UpdateRestaurantButton.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        UpdateRestaurantButton.setText("Τροποποίηση");
        UpdateRestaurantButton.setEnabled(false);
        UpdateRestaurantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateRestaurantButtonActionPerformed(evt);
            }
        });
        BackgroundColorPanel.add(UpdateRestaurantButton);
        UpdateRestaurantButton.setBounds(10, 79, 153, 21);

        DeleteRestaurantButton.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        DeleteRestaurantButton.setText("Διαγραφή Εστιατορίου");
        DeleteRestaurantButton.setEnabled(false);
        DeleteRestaurantButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteRestaurantButtonActionPerformed(evt);
            }
        });
        BackgroundColorPanel.add(DeleteRestaurantButton);
        DeleteRestaurantButton.setBounds(10, 120, 153, 21);

        ExitButton.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        ExitButton.setText("Έξοδος");
        ExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitButtonActionPerformed(evt);
            }
        });
        BackgroundColorPanel.add(ExitButton);
        ExitButton.setBounds(10, 161, 153, 21);

        getContentPane().add(BackgroundColorPanel);
        BackgroundColorPanel.setBounds(0, 126, 180, 500);

        TitleLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        TitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        TitleLabel.setText("Τροποποίηση Εστιατορίου");
        getContentPane().add(TitleLabel);
        TitleLabel.setBounds(410, 50, 310, 30);

        UpdateTable.setBackground(new java.awt.Color(255, 255, 204));
        UpdateTable.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        UpdateTable.setModel(setRestaurantTableModel());
        UpdateTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                UpdateTablePropertyChange(evt);
            }
        });
        UpdateScrollPane.setViewportView(UpdateTable);

        getContentPane().add(UpdateScrollPane);
        UpdateScrollPane.setBounds(190, 162, 500, 460);

        UpdateButton.setText("Τροποποίηση");
        UpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateButtonActionPerformed(evt);
            }
        });
        getContentPane().add(UpdateButton);
        UpdateButton.setBounds(320, 633, 170, 40);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddRestaurantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddRestaurantButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_AddRestaurantButtonActionPerformed

    private void UpdateRestaurantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateRestaurantButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_UpdateRestaurantButtonActionPerformed

    private void DeleteRestaurantButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteRestaurantButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_DeleteRestaurantButtonActionPerformed

    private void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitButtonActionPerformed
        AdministratorMainGui.loggedUser= this.loggedUser;
        JFrame AdministratorMainForm = new AdministratorMainGui();
        AdministratorMainForm.setBounds(50, 100, 800, 800);
        AdministratorMainForm.setLocationRelativeTo(null); // possition center of screen
        AdministratorMainForm.setResizable(false); // not resizeable
        AdministratorMainForm.setVisible(true); // show AdministratorMain
        dispose();
}//GEN-LAST:event_ExitButtonActionPerformed

    private void UpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateButtonActionPerformed
       UpdateSelectedFromTable();
                
}//GEN-LAST:event_UpdateButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        TerminateQuestion.windowClosing(this);
    }//GEN-LAST:event_formWindowClosing

    private void UpdateTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_UpdateTablePropertyChange
        //εαν δεν υπάρχουν γραμμές στον πίνακα 
        // απενεργοποιώ το κουμπί UpdateButton
        if ((UpdateTable.getModel().getRowCount()==0)){
            UpdateTable.setEnabled(false);
        } else {
            UpdateTable.setEnabled(true);
        }
    }//GEN-LAST:event_UpdateTablePropertyChange

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdminUpdateRestaurantGui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddRestaurantButton;
    private javax.swing.JPanel BackgroundColorPanel;
    private javax.swing.JButton DeleteRestaurantButton;
    private javax.swing.JButton ExitButton;
    private javax.swing.JLabel LogoLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JButton UpdateButton;
    private javax.swing.JButton UpdateRestaurantButton;
    private javax.swing.JScrollPane UpdateScrollPane;
    private javax.swing.JTable UpdateTable;
    // End of variables declaration//GEN-END:variables

    private TableModel setRestaurantTableModel(){
        return AllRestaurants.forTable(AllRestaurants.getArrayList());
    }
    
     private void UpdateSelectedFromTable(){
        // ανάκληση επιλλεγμένων γραμμών από τον πίνακα
        int[] rowIndices ;
        int rID;
        // όλες τις επιλεγμένες γραμμές στον πίνακα rowIndices
        rowIndices = UpdateTable.getSelectedRows();
        //εαν υπάρχουν επιλεγμένες γραμμές
        if (!(rowIndices.length==0)){
                // διάβασε το ID του εστιατορίου
                // βρίσκεται στην πρώτη στήλη του πίνακα
                // η οποία δεν εμφανίζεται στην φόρμα
                rID = (int) UpdateTable.getModel().getValueAt(rowIndices[0], 0);
                AdminUpdateRestaurant2Gui.currentRestaurantID=rID;
                JFrame UpdateRestaurant2Form = new AdminUpdateRestaurant2Gui();
                UpdateRestaurant2Form.setBounds(50, 100, 800, 800);
                UpdateRestaurant2Form.setLocationRelativeTo(null); // possition center of screen
                UpdateRestaurant2Form.setResizable(false); // not resizeable
                UpdateRestaurant2Form.setVisible(true); // show MainForm - login screen
                dispose();
        } else {
            // μήνυμα μη επιλογής γραμμής στον πίνακα
            InformationMessage.show(this,"Πρέπει να επιλέξετε ενα εστιατόριο από τον πίνακα");
        }
        
     }
     
     private void hideIdFromTable(){
         // αφαιρώ την πρώτη στήλη από τον πίνακα, ώστε το ID να μην εμφανίζεται
        TableColumn tcDeleteTable = UpdateTable.getColumnModel().getColumn(0);
        UpdateTable.getColumnModel().removeColumn(tcDeleteTable);
        // ρύθμιση μονής επιλογής στον πίνακα 
        UpdateTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
     }
}
